@node Pessimistic concurrency
@section Pessimistic concurrency

@cindex mutual exclusion
@cindex mutex locks
@cindex locks for mutual exclusion
While Scheme48's primitive thread synchronization mechanisms revolve
around optimistic concurrency, Scheme48 still provides the more
well-known mechanism of pessimistic concurrency, or mutual exclusion,
with locks.  Note that Scheme48's pessimistic concurrency facilities
are discouraged, and very little of the system uses them (at the time
this documentation was written, none of the system uses locks), and the
pessimistic concurrency libraries are limited to just locks; condition
variables are integrated only with optimistic concurrency.  Except for
inherent applications of pessimistic concurrency, it is usually better
to use optimistic concurrency in Scheme48.

@stindex locks
These names are exported by the @code{locks} structure.

@deffn procedure make-lock @returns{} lock
@deffnx procedure lock? @returns{} boolean
@deffnx procedure obtain-lock lock @returns{} unspecified
@deffnx procedure maybe-obtain-lock lock @returns{} boolean
@deffnx procedure release-lock lock @returns{} unspecified
@code{Make-lock} creates a new lock in the `released' lock state.
@code{Lock?} is the disjoint type predicate for locks.
@code{Obtain-lock} atomically checks to see if @var{lock} is in the
`released' state: if it is, @var{lock} is put into the `obtained' lock
state; otherwise, @code{obtain-lock} waits until @var{lock} is ready to
be obtained, at which point it is put into the `obtained' lock state.
@code{Maybe-obtain-lock} atomically checks to see if @var{lock} is in
the `released' state: if it is, @var{lock} is put into the `obtained'
lock state, and @code{maybe-obtain-lock} returns @code{#t}; if it is in
the `obtained' state, @code{maybe-obtain-lock} immediately returns
@code{#f}.  @code{Release-lock} sets @var{lock}'s state to be
`released,' letting the next thread waiting to obtain it do so.
@end deffn
